<template>
	<view>
		<view class="backBox">
			<view class="iconBox" @click="back">
				<image src="../../static/mine_1.png"></image>
				<text>提交订单</text>
			</view>
		</view>
		<view class="container position-relative">
			
			<view style="margin-bottom: 130rpx;">
				<view class="section-1">
					<template >
						<list-cell @click="chooseAddress" v-if="addressInfo">
							<view class="w-100 d-flex flex-column">
								<view class="d-flex align-items-center justify-content-between ">
									<view class="font-size-extra text-color">填写收货地址</view>
									<image src="/static/images/navigator-1.png" class="arrow"></image>
								</view>
								
							</view>
						</list-cell>
						<list-cell @click="chooseAddress" v-else>
							<view class="w-100 d-flex flex-column">
								<view class="d-flex align-items-center justify-content-between mb-10">
									<view class="font-size-extra-lg text-color-B">{{address.addr}} {{address.houseNumber}}</view>
									<image src="/static/images/navigator-1.png" class="arrow"></image>
								</view>
								<view class="d-flex text-color-assist font-size-sm align-items-center">
									<view class="mr-10">{{ address.receiver }}</view>
									<view class="mr-10">{{ !address.gender ? '女士' : '先生' }}</view>
									<view>{{ address.mobile }}</view>
								</view>
							</view>
						</list-cell>
					</template>
					<template>
						
						<list-cell class="contact" last :hover="false" v-if="!addressInfo">
							<view class="flex-fill d-flex justify-content-between align-items-center">
								<view class="title flex-fill">联系电话</view>
								<view class="time">
									<input class="text-right" placeholder="请输入手机号码" disabled :value="address.mobile" placeholder-style="fontSize:14px;color:#999999;" />
								</view>
								<!-- <view class="contact-tip font-size-sm">自动填写</view> -->
							</view>
						</list-cell>
					</template>
					
				</view>
				<!-- 购物车列表 begin -->
				<view class="section-2">
					<view class="cart d-flex flex-column">
						<list-cell last v-for="(item, index) in cart" :key="index">
							<view class="w-100 d-flex flex-column">
								<view class="d-flex align-items-center mb-10">
									<view class="name-and-props overflow-hidden">
										<view class="text-color-B font-size-lg">
											{{ item.name }}
										</view>
									</view>
									<view class="d-flex flex-fill justify-content-between align-items-center text-color-base font-size-lg">
										<view>x{{ item.number }}</view>
										<view style="color: #8d4a32;">￥{{ item.price }}</view>
									</view>
								</view>
								<view class="text-truncate font-size-base text-color-assist">
									{{ item.showUnit }} {{ item.showGroup }}
								</view>
							</view>
						</list-cell>
						<template>
							<list-cell last >
								<view class="w-100 d-flex font-size-base align-items-center justify-content-between">
									<view>打包费</view>
									<view>￥{{ takeoutPackagingFee.toFixed(2) }}</view>
								</view>
							</list-cell>
							<list-cell last >
								<view class="w-100 d-flex font-size-base align-items-center justify-content-between">
									<view>配送费</view>
									<view>￥{{ minDeliveryFee }}</view>
								</view>
							</list-cell>
							<list-cell last >
								<view class="w-100 d-flex font-size-base align-items-center justify-content-between">
									<view>配送时间</view>
									<view>立即配送</view>
								</view>
							</list-cell>
						</template>
					</view>
					
					<list-cell  >
						<view class="flex-fill d-flex justify-content-between align-items-center">
							<view class="text-color-base">福币抵扣</view>
							<view class="text-ip" v-if="balance > total">{{fbdk.toFixed(2) || 0}}</view>
							<view class="text-ip" v-else>{{balance || 0}}</view>
						</view>
					</list-cell>
					
					<list-cell last>
						<view class="flex-fill d-flex justify-content-end align-items-center">
							<view>总计￥{{ totalPrice.toFixed(2) || 0}},实付</view>
							<view class="font-size-extra-lg font-weight-bold" style="color: #000;" v-if="fbDeduction">￥0</view>
							<view class="font-size-extra-lg font-weight-bold" style="color: #000;" v-else>￥{{  total.toFixed(2) || 0 }}</view>
						</view>
					</list-cell>
				</view>
				<!-- 购物车列表 end -->
				<view class="d-flex align-items-center justify-content-start font-size-sm text-color-warning" 
					style="height: 15px;">
				
				</view>
				
				<!-- 备注 begin -->
				<list-cell arrow last @click="goToRemark">
					<view class="d-flex flex-fill align-items-center justify-content-between overflow-hidden">
						<view class="flex-shrink-0 mr-20">备注</view>
						<view class="text-color-primary flex-fill text-truncate text-right">{{ remark || '点击填写备注' }}</view>
					</view>
				</list-cell>
				<!-- 备注 end -->
				
				
			</view>
			<view style="height: 50px;width: 100%;"></view>
			<!-- 付款栏 begin -->
			<view class="w-100 pay-box position-fixed fixed-bottom d-flex align-items-center justify-content-between bg-white">
				<view class="font-size-sm" style="margin-left: 20rpx;">超出福币金额：</view>
				<view class="font-size-lg flex-fill" v-if="fbDeduction">￥<text style="font-size: 19px;font-weight: bold;" >0</text></view>
				<view class="font-size-lg flex-fill" v-else>￥<text style="font-size: 19px;font-weight: bold;" >{{  total.toFixed(2)  }}</text></view>
				<view class=" h-100 d-flex align-items-center just-content-center text-color-white font-size-base"
					style="padding: 0 80rpx;background: #8d4a32;font-size: 18px;" @tap="submit">
					提交订单
				</view>
			</view>
			
		</view>
	</view>
	
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	import modal from '@/components/modal/modal'
	
	export default {
		components: {
			listCell,
			modal
		},
		data() {
			return {
				cart: [],
				remark: '',//订单备注
				ensureAddressModalVisible: false,
				address:{addrId:''},
				shopId:'',
				items:[],
				takeoutPackagingFee:0,//打包费
				shop:{},//商家信息
				minDeliveryFee:0,//配送费
				addressInfo:false,
				balance:0,//福币余额
				clickFlag:false,
				fbDeduction:false,//福币是否抵扣全部
				fbdk:0,
			}
		},
		computed: {
			//计算总价
			totalPrice() {
				var price = Number(this.cart.reduce((acc, cur) => acc + cur.number * cur.price + cur.number * cur.takeoutPackagingFee, 0));
				var total = Number(this.minDeliveryFee) + price;
				return total
				 
			},
			//计算实付价格
			total() {
				var price = Number(this.cart.reduce((acc, cur) => acc + cur.number * cur.price + cur.number * cur.takeoutPackagingFee, 0));
				var total = Number(this.minDeliveryFee) + price;
				var balance = Number(this.balance)
				if(balance > total){
					this.fbDeduction = true
					this.fbdk = total
					return total
				}else{
					this.fbdk = balance;
					return total - balance
				}
				 
			},
		},
		onLoad(e) {
			this.$wxHide()
			if(e.data){
				this.cart = JSON.parse(e.data)
				this.shopId = e.shopId
				this.getShop()
				//解析前台购物车数组 
				for (var i = 0; i < this.cart.length; i++) {
					this.takeoutPackagingFee = Number(this.cart[i].takeoutPackagingFee)*this.cart[i].number + this.takeoutPackagingFee
					var obj = {
						foodId:this.cart[i].id,
						foodUnitId:this.cart[i].unitKey,
						number:this.cart[i].number,
						remark:this.cart[i].group,
					}
					this.items.push(obj)
				}
			}
			this.getUserInfo()
			this.getAdderss()
		},
		onShow() {
			const remark = uni.getStorageSync('remark')
			if(remark){
				this.remark = remark;
			}
			
			const address = uni.getStorageSync('changeAddress')
			if(address){
				this.address = address;
				this.addressInfo = false;
				uni.removeStorageSync('changeAddress')
			}
			
		},
		methods: {
			back(){
				uni.navigateBack()
			},
			//获取余额信息
			getUserInfo() {
				uni.showLoading({
					title:'加载中'
				})
				this.uni_request({
					url: '/api/user/getUserBalance',
					method: 'POST',
					success: (res) => {
						uni.hideLoading()
						if (res.data.code == 0 || res.data.code == 200) {
							this.balance = Number(res.data.data)
							this.$forceUpdate()
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							setTimeout(function() {
								uni.navigateBack()
							}, 1000);
						}
					},
				})
				
			},
			//查询商家配送费等信息
			getShop(){
				var that = this;
				that.uni_request({
					url: '/api/shop/' + this.shopId,
					method: 'POST',
					success: (res) => {
						
						if (res.data.code == 0 || res.data.code == 200) {
							that.minDeliveryFee = res.data.data.minDeliveryFee
						} 
					},
					fail(e){
						uni.hideLoading()
					},
				})
			},
			//查询默认地址
			getAdderss(){
				var that = this;
				
				that.uni_request({
					url: '/api/address/defaultAddress',
					method: 'POST',
					success: (res) => {
						
						if (res.data.code == 0 || res.data.code == 200) {
							that.address = res.data.data
							if(that.address && that.address.addrId){
								that.addressInfo = false
							}else{
								that.addressInfo = true
							}
							
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail(e){
						uni.hideLoading()
					},
				})
			},
			//提交订单
			submit() {
				var that = this;
				
				if(!that.address || !that.address.addrId){
					uni.showToast({
						icon:'none',
						title:'请填写收货地址'
					})
					return
				}
				
				var params = {
					addressId:that.address.addrId,
					shopId:that.shopId,
					orderAmount:Number(that.totalPrice).toFixed(2),
					remark:that.remark,
					items:that.items,
					wxPayAmount:Number(that.total).toFixed(2),
					fbPayAmount: Number(that.fbdk).toFixed(2)
				}
				
				if(that.fbDeduction){
					params.wxPayAmount = 0
				}
				
				uni.showLoading({
					title:'加载中',
					mask:true
				})
				that.uni_request({
					url: '/api/order',
					method: 'POST',
					data:params,
					success: (res) => {
						
						
						if (res.data.code == 0 || res.data.code == 200) {
							// uni.showToast({
							// 	title: res.data.msg,
							// 	icon: 'none'
							// })
							if(that.fbDeduction){
								that.balancePay(res.data.orderId)
								
							}else{
								uni.hideLoading()
								setTimeout(function() {
									uni.navigateTo({
										url: '/pages/pay/orderPay?id='+res.data.orderId
									})
								}, 1000);
							}
							
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					fail(e){
						uni.hideLoading()
					},
				})
			},
			goToRemark() {
				uni.navigateTo({
					url: '/pages/remark/remark?remark=' + this.remark
				})
			},
			chooseAddress() {
				uni.navigateTo({
					url: '/pages/address/address?is_choose=true&scene=pay'
				})
			},
			
			
			//福币支付
			balancePay(id){
				var that = this;
				if(that.clickFlag){
					return
				}
				that.clickFlag = true;
				that.uni_request({
					url: '/api/pay/fb',
					method: 'POST',
					data:{
						orderId:id,
					},
					success: (res) => {
						uni.hideLoading()
						that.clickFlag = false
						
						if (res.data.code == 0 || res.data.code == 200) {
							uni.showToast({
								icon:'success',
								title: res.data.msg,
							})
							setTimeout(function() {
								uni.switchTab({
									url:'/pages/take-foods/take-foods'
								})
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.backBox{
		width: 100vw;
		height: 90px;
		background: $base;
		.iconBox{
			height: 60px;
			@extend %flex-align-center;padding: 0 10px;
			font-size: 17px;color: #fff;letter-spacing: 1px;font-weight: bold;
			image{
				width: 20px;height: 20px;margin-right: 8px;margin-top: 1px;
			}
		}
		
	}
	
	.text-ip{
		color: $base;font-weight: bold;font-size: 15px;
	}
	.text-color-B{
		color: #000 !important;
	}
	.container {
		padding: 0 10px;margin-top: -30px;
	}
	.font-size-extra{
		font-size: 16px;
	}
	.arrow {
		width: 50rpx; 
		height: 50rpx;
		position: relative;
		margin-right: -10rpx;
	}
	
	.location {
		.store-name {
			font-size: $font-size-lg;
		}
		
		.iconfont {
			font-size: 50rpx;
			line-height: 100%;
			color: $color-primary;
		}
	}
	
	.section-1 {
		margin-bottom: 30rpx;
		.contact {
			.contact-tip {
				margin-left: 10rpx;
				border: 2rpx solid $color-primary;
				padding: 6rpx 10rpx;
				color: $color-primary;
			}
		}
	}
	
	.section-2 {
		.name-and-props {
			width: 65%;
		}
	}
	
	.payment {
		margin-bottom: 30rpx;
		
		.disabled {
			color: $text-color-grey;
		}
		
		.payment-icon {
			font-size: 44rpx; 
			margin-right: 10rpx;
		}
		
		.checkbox {
			font-size: 36rpx;
			margin-left: 10rpx;
		}
		
		.checked {
			color: $color-primary;
		}
	}
	
	.pay-box {
		box-shadow: 0 0 20rpx rgba(0, 0, 0, .1);
		height: 100rpx;
	}
	
	.modal-content {
		.change-address-btn {
			line-height: 2;
			padding: 0 1em;
		}
		.pay_btn {
			width: 100%;
			border-radius: 50rem !important;
			line-height: 3;
		}
	}
</style>
